package zgrjb.snbq.teach_platform.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.service.TaskQueueService;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.time.LocalDateTime;

/**
 * 任务队列管理控制器
 * 
 * @author system
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/api/v1/task")
@Slf4j
public class TaskQueueController {

    @Autowired
    private TaskQueueService taskQueueService;

    /**
     * 获取任务列表
     */
    @GetMapping("/list")
    public JsonData getTasks(@RequestParam(required = false) String taskType,
                           @RequestParam(required = false) Integer status,
                           @RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "10") int size) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.getUserTasks(user.getId(), taskType, status, page, size);
    }

    /**
     * 创建任务
     */
    @PostMapping("/create")
    public JsonData createTask(@RequestBody CreateTaskRequest request) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.createTask(
            user.getId(),
            request.getTaskType(),
            request.getTitle(),
            request.getDescription(),
            request.getSourceId(),
            request.getSourceType(),
            request.getPriority()
        );
    }

    /**
     * 更新任务状态
     */
    @PostMapping("/status/{taskId}")
    public JsonData updateTaskStatus(@PathVariable Long taskId, 
                                   @RequestBody UpdateTaskStatusRequest request) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.updateTaskStatus(taskId, request.getStatus(), user.getId());
    }

    /**
     * 完成任务
     */
    @PostMapping("/complete/{id}")
    public JsonData completeTask(@PathVariable Long id) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.completeTask(id, user.getId());
    }

    /**
     * 获取任务统计
     */
    @GetMapping("/statistics")
    public JsonData getStatistics() {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.getTaskStatistics(user.getId());
    }

    /**
     * 获取即将到期的任务
     */
    @GetMapping("/upcoming")
    public JsonData getUpcomingTasks(@RequestParam(defaultValue = "7") int days) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.getUpcomingTasks(user.getId(), days);
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/{taskId}")
    public JsonData getTaskDetail(@PathVariable Long taskId) {
        User user = LoginInterceptor.getCurrentUser();
        return taskQueueService.getTaskDetail(taskId, user.getId());
    }

    /**
     * 自动生成批改任务（教师功能）
     */
    @PostMapping("/auto-generate/grading/{examId}")
    public JsonData autoGenerateGradingTasks(@PathVariable Long examId) {
        // 可以添加权限验证，确保只有教师可以调用
        return taskQueueService.autoGenerateGradingTasks(examId);
    }

    /**
     * 清理过期任务（管理员功能）
     */
    @PostMapping("/clean-expired")
    public JsonData cleanExpiredTasks() {
        // 可以添加权限验证，确保只有管理员可以调用
        return taskQueueService.cleanExpiredTasks();
    }

    /**
     * 创建任务请求体
     */
    public static class CreateTaskRequest {
        private String taskType;
        private String title;
        private String description;
        private Long sourceId;
        private String sourceType;
        private Integer priority;
        private LocalDateTime dueTime;

        // Getters and Setters
        public String getTaskType() { return taskType; }
        public void setTaskType(String taskType) { this.taskType = taskType; }
        
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
        
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        
        public Long getSourceId() { return sourceId; }
        public void setSourceId(Long sourceId) { this.sourceId = sourceId; }
        
        public String getSourceType() { return sourceType; }
        public void setSourceType(String sourceType) { this.sourceType = sourceType; }
        
        public Integer getPriority() { return priority; }
        public void setPriority(Integer priority) { this.priority = priority; }
        
        public LocalDateTime getDueTime() { return dueTime; }
        public void setDueTime(LocalDateTime dueTime) { this.dueTime = dueTime; }
    }

    /**
     * 更新任务状态请求体
     */
    public static class UpdateTaskStatusRequest {
        private Integer status;

        public Integer getStatus() { return status; }
        public void setStatus(Integer status) { this.status = status; }
    }
} 